package com.example.demo.aspect;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;

@Slf4j
@Aspect
@Component
public class TimeRecordAspect {
//    aroud必须要有返回结果
    @Around("execution(* com.example.demo.Controller.*.*(..))")
    public Object timeRecord(ProceedingJoinPoint joinPoint){
//      1.记录时间
//      2.找到执行方法
//      3.记录结束时间
//      4.记录耗时
        long start = System.currentTimeMillis();
        log.info(joinPoint.getSignature().getName());
        log.info("do aroud  前");
        Object result = null;
        try {
            result = joinPoint.proceed();
        } catch (Throwable e) {
            throw new RuntimeException(e);
        }
        log.info("do around后");
        log.info(joinPoint.getSignature()+"耗时："+(System.currentTimeMillis()-start)+"ms" );
        return result;
    }

    @Before("execution(* com.example.demo.Controller.*.*(..))")
    public void timeReccord2(JoinPoint joinPoint){
        log.info(joinPoint.getSignature().getName());
        log.info("执行before方法");
    }

    @AfterReturning("execution(* com.example.demo.Controller.*.*(..))")
    public void timeReccord3(JoinPoint joinPoint){
        log.info(joinPoint.getSignature().getName());
        log.info("");
    }
}
